package me.roger.zen.model;

import java.util.ArrayList;

import me.roger.zen.application.ZenApplication;
import me.roger.zen.data.ZenTopicData;
import me.roger.zen.utils.ZenURLConnection;
import me.roger.zen.utils.ZenURLConnection.ZenOnResponseListener;
import me.roger.zen.utils.ZenUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.content.Intent;

public class ZenHotModel {
	public static final String DidFinishedLoad = "ZenHotDidFinishedLoad";
	public static final String DidFailedLoad = "ZenHotDidFailedLoad";

	public static int ZEN_TOPIC_LIST = 1001;
	public static int ZEN_TOPIC_FAV = 1002;

	public ArrayList<ZenTopicData> topics;

	private static final String ZEN_HOT_URL = "http://bbs.hupu.com";

	private ZenURLConnection mConnection;
	private Context mContext;
	
	public ZenHotModel() {
		mContext = ZenApplication.getAppContext();
		topics = new ArrayList<ZenTopicData>();
	}

	public void cancel() {
		if (mConnection != null) {
			mConnection.cancel();
		}
	}

	private int parser(String response) {

		try {
			System.out.println("hot response: " + response);
			Document doc = Jsoup.parse(response);
			Elements nodes = doc.select("div#recomBbsIndex div.list ul li");
			System.out.println("nodes: " + nodes.size());
			if (nodes != null && nodes.size() > 0) {
				for (int i = 0; i < nodes.size(); i++) {
					
					ZenTopicData topic = new ZenTopicData();
					Element node = nodes.get(i);
					Elements titles = node.select("span.textSpan a");
					if (titles != null && titles.size() > 0) {
						Element title = titles.get(0);
						String url = title.attr("href");
						String tid = ZenUtils.sub("/", ".html",
								url);
						topic.tid = tid;
						topic.subject = title.text();
					}
					
					Elements replies = node.select("span.textSpan em");
					if (replies != null && replies.size() > 0) {
						Element reply = replies.get(0);
						topic.replies = reply.text();
					} 
					
					Elements boards= node.select("span.forum a");
					
					if (boards != null && boards.size() > 0) {
						Element board = boards.get(0);
						topic.board = board.text();
					}
					topics.add(topic);
				}
			}
			
			return 0;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return -1;
	}

	public void load() {

		mConnection = new ZenURLConnection(ZEN_HOT_URL);
		mConnection.setReadCharsetName("gb2312");
		mConnection.addRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
		mConnection.setOnResponseListener(new ZenOnResponseListener() {

			@Override
			public void OnResponse(String response) {
				if (response != null) {
					int result = parser(response);
					if (result == 0) {
						mContext.sendBroadcast(new Intent(DidFinishedLoad));
					} else {
						mContext.sendBroadcast(new Intent(DidFailedLoad));
					}
				}
			}

			@Override
			public void OnError(String msg) {
				mContext.sendBroadcast(new Intent(DidFailedLoad));
			}
		});
		mConnection.startAsychronous();
	}
}
